package cn.wf.apigateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 * @Description  跨域配置(没有测试)
 * @Author 万飞
 * @Date 2018/8/19 10:06
 * C - Cross  O - Origin  R - Resource  - S - Sharing(跨域资源共享)
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);  //支持cookie跨域
        config.setAllowedOrigins(Arrays.asList("*"));  //http://www.a.com
        config.setAllowedHeaders(Arrays.asList("*"));
        config.setAllowedMethods(Arrays.asList("*"));
        config.setMaxAge(300l);  //300秒  缓存时间，这个时间段内，相同的跨域请求，不在进行检查

        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
